<template>
  <el-row class="warp">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>首页</b></el-breadcrumb-item>
        <el-breadcrumb-item>企业列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>

    <el-col :span="24" class="warp-main" v-loading="loading" element-loading-text="拼命加载中">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
          <el-form-item>
            <el-input v-model="filters.name" placeholder="企业名称" style="min-width: 240px;"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="showAddDialog">添加</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <!--列表-->
      <el-table :data="enterpriseInfo" highlight-current-row v-loading="loading" style="width: 100%;">
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column label="序号" prop="id" width="80">
        </el-table-column>
        <el-table-column prop="name" label="企业名称" width="120" sortable>
        </el-table-column>
        <el-table-column prop="synopsis" label="企业简介" width="180" sortable>
        </el-table-column>
        <el-table-column prop="address" label="所在地址" width="180"sortable>
        </el-table-column>
        <el-table-column prop="state" label="数据状态" min-width="20">
          <template slot-scope="scope">
            <el-switch
              active-text="启用"
              inactive-text="禁用" v-model="scope.row.state" :active-value="1" :inactive-value="0" @change="stateChange(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="320">
          <template slot-scope="scope" >
            <el-button size="mini" @click="detail(scope.row.id)">详细信息</el-button>
            <el-button size="mini" @click="editGetById(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleted(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                       style="float:right;">
        </el-pagination>
      </el-col>

      <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false" width="50%">
        <el-form :model="addForm" :rules="rules" label-width="100px" ref="addForm">
          <el-form-item label="企业名称" prop="name">
            <el-input v-model="addForm.name" auto-complete="off" placeholder="输入企业名称"></el-input>
          </el-form-item>
          <el-form-item label="简介" prop="synopsis">
            <el-input type="textarea":rows="2" placeholder="输入企业简介" v-model="addForm.synopsis" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="企业负责人" prop="personLiable">
            <el-input v-model="addForm.personLiable" auto-complete="off" placeholder="输入企业负责人"></el-input>
          </el-form-item>
          <el-form-item label="负责人身份证" prop="personLiableId">
            <el-input v-model="addForm.personLiableId" auto-complete="off" placeholder="输入负责人身份证"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="tel">
            <el-input v-model="addForm.tel" auto-complete="off" placeholder="输入联系电话"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="addForm.address" auto-complete="off" placeholder="输入企业地址"></el-input>
          </el-form-item>
          <el-form-item label="企业分润" prop="profitRetio">
            <el-input v-model="addForm.profitRetio" auto-complete="off" placeholder="输入企业分润百分比"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="addFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
        </div>
      </el-dialog>


      <el-dialog title="编辑" :visible.sync="editVisible" :close-on-click-modal="false" width="50%">
        <el-form :model="editForm" :rules="rules" label-width="100px" ref="editForm">
          <el-form-item label="企业名称" prop="name">
            <el-input v-model="editForm.name" auto-complete="off" placeholder="输入企业名称"></el-input>
          </el-form-item>
          <el-form-item label="简介" prop="synopsis">
            <el-input type="textarea":rows="2" placeholder="输入企业简介" v-model="editForm.synopsis" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="企业负责人" prop="personLiable">
            <el-input v-model="editForm.personLiable" auto-complete="off" placeholder="输入企业负责人"></el-input>
          </el-form-item>
          <el-form-item label="负责人身份证" prop="personLiableId">
            <el-input v-model="editForm.personLiableId" auto-complete="off" placeholder="输入负责人身份证"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="tel">
            <el-input v-model="editForm.tel" auto-complete="off" placeholder="输入联系电话"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="editForm.address" auto-complete="off" placeholder="输入企业地址"></el-input>
          </el-form-item>
          <el-form-item label="企业分润" prop="profitRetio">
            <el-input v-model="editForm.profitRetio" auto-complete="off" placeholder="输入企业分润百分比"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="editVisible = false">取消</el-button>
          <el-button type="primary" @click.native="editSubmit" :loading="addLoading">提交</el-button>
        </div>
      </el-dialog>

      <el-dialog title="详细信息" :visible.sync="detailVisible" :close-on-click-modal="false" width="50%">
        <el-form :model="detailForm" label-width="100px" ref="detailForm">
          <el-form-item label="企业名称" prop="name">
            <el-input v-model="detailForm.name" auto-complete="off" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="简介" prop="synopsis">
            <el-input type="textarea":rows="2" :disabled="true" v-model="detailForm.synopsis" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="企业负责人" prop="personLiable">
            <el-input v-model="detailForm.personLiable" auto-complete="off" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="负责人身份证" prop="personLiableId">
            <el-input v-model="detailForm.personLiableId" auto-complete="off" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="tel">
            <el-input v-model="detailForm.tel" auto-complete="off" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="detailForm.address" auto-complete="off" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="企业分润" prop="profitRetio">
            <el-input v-model="detailForm.profitRetio" auto-complete="off" :disabled="true"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="detailVisible = false">返回</el-button>
        </div>
      </el-dialog>
    </el-col>
  </el-row>
</template>

<script>
  import API_ESCORT_ENTERPRISE from "../../api/api_escort_enterprise";
  import {isInteger,phoneNumber,checkFloat,validateIdNo,checkRatio} from "../../common/validate";

  export default {
    data() {
      return {
        rules: {
          name: [
            {required: true, message: "请输入姓名", trigger: "blur"}
          ],
          synopsis:[
            {required: true, message: "请输入简介", trigger: "blur"}
          ],
          tel:[
            {required: true, message: "请输入电话", trigger: "blur"},
            {validator: phoneNumber, trigger: 'blur'}
          ],
          personLiableId:[
            {required:true,message:"请输入负责人身份证号",trigger:"blur"},
            {validator: validateIdNo, trigger: 'blur'}
          ],
          personLiable:[
            {required:true,message:"请输入企业负责人",trigger:"blur"},
          ],
          address:[
            {required:true,message:"请输入地址",trigger:"blur"},
          ],
          profitRetio:[
            {required:true,message:"请输入企业分润/百分比",trigger:"blur"},
            {validator: checkRatio, trigger: 'blur'}
          ]
        },
        filters: {
          name: ""
        },
        loading: false,
        enterpriseInfo:[],
        total: 0,
        page: 1,
        limit: 10,
        addFormVisible: false,
        addLoading: false,
        dialogVisible: false,
        editVisible:false,
        detailVisible:false,
        addForm: {
          name: "",
          synopsis: "",
          personLiable:"",
          personLiableId:"",
          tel:"",
          address: "",
          longitude: "",
          latitude: "",
          profitRetio:""
        },
        editForm: {
          name: "",
          synopsis: "",
          personLiable:"",
          personLiableId:"",
          tel:"",
          address: "",
          longitude: "",
          latitude: "",
          profitRetio:""
        },
        detailForm: {
          name: "",
          synopsis: "",
          personLiable:"",
          personLiableId:"",
          tel:"",
          address: "",
          longitude: "",
          latitude: "",
          profitRetio:""
        }
      };
    },
    methods: {
      handleCurrentChange(val) {
        this.page = val;
        this.search();
      },
      handleSearch() {
        this.total = 0;
        this.page = 1;
        this.search();
      },
      //获取企业列表
      search:function () {
        console.log("search")
        let that = this;
        let params = {
          page: that.page,
          limit: 10,
          name: that.filters.name
        };
        that.loading = true;
        API_ESCORT_ENTERPRISE.findList(params)
          .then(
            function (result) {
            console.log(result);
            that.loading = false;
            if (result.code != 0){
              that.$message.error({
                showClose: true,
                message: result.msg,
                duration: 2000
              });
            } else if (result && result.page.rows) {
              that.total = result.page.total;
              that.enterpriseInfo = result.page.rows;
            }
            else{
              that.total = 0;
              that.enterpriseInfo = [];
            }
          },function (err) {
              that.loading = false;
              that.$message.error({
                showClose: true,
                message: err.toString(),
                duration: 2000
              });
            })
          .catch(function (error) {
            that.loading = false;
            console.log(error);
            that.$message.error({
              showClose: true,
              message: "请求出现异常",
              duration: 2000
            });
          })
      },
      addSubmit:function () {
        console.log("addSubmit");
        let that = this;
        this.$refs.addForm.validate(valid => {
          if (valid) {
            that.loading = true;
            let params = Object.assign({}, this.addForm);
            API_ESCORT_ENTERPRISE.addEnterprise(params).then(function (result) {
              if (0 === result.code) {
                that.loading = false;
                that.$message;
                that.$message.success({
                  showClose: true,
                  message: "新增成功",
                  duration: 2000
                });
                that.$refs["addForm"].resetFields();
                that.addFormVisible = false;
                that.search();
              } else {
                that.$message.error({
                  showClose: true,
                  message: "新增失败",
                  duration: 2000
                });
              }
            });
          }
        })
      },
      editGetById:function(data){
        console.log("editGetById");
        let that = this;
        that.loading = true;
        let param = {
          id : data
        }
        API_ESCORT_ENTERPRISE.getById(param).then(function (result) {
          that.loading = false;
          console.log(result);
          if (0 == result.code){
            that.editForm = result.info;
            that.editVisible = true;
          } else{
            that.$message.error({
              showClose: true,
              message: "请求数据失败",
              duration: 2000
            });
          }
        }).catch(function (err) {
          that.loading = false;
          console.log(error);
          that.$message.error({
            showClose: true,
            message: "请求出现异常",
            duration: 2000
          });
        });
      },
      editSubmit:function(){
        console.log("editSubmit");
        let that = this;
        that.$refs.editForm.validate(valid=>{
          if (valid){
            this.loading = true;
            let param = Object.assign({},that.editForm);
            API_ESCORT_ENTERPRISE.editEnterprise(param).then(function (result) {
              console.log(result);
              that.editVisible = false;
              if (0 === result.code) {
                that.loading = false;
                that.$message;
                that.$message.success({
                  showClose: true,
                  message: "编辑成功",
                  duration: 2000
                });
                that.$refs["editForm"].resetFields();
                that.editFormVisible = false;
                that.search();
              } else {
                that.$message.error({
                  showClose: true,
                  message: "编辑失败",
                  duration: 2000
                });
              }
            });
          }
        })
      },
      showAddDialog: function () {
        this.addFormVisible = true;
      },
      stateChange: function (data) {
        let that = this;
        that.loading = true;
        API_ESCORT_ENTERPRISE.changeState(data).then(
          function (result) {
          if (0 === result.code) {
            that.loading = false;
            that.$message;
            that.$message.success({
              showClose: true,
              message: "修改成功",
              duration: 2000
            });
          }else {
            that.$message.error({
              showClose: true,
              message: "修改失败",
              duration: 2000
            });
          }
        },function (err) {
            console.log(err)
            that.loading = false;
            that.$message.error({
              showClose: true,
              message: err,
              duration: 2000
            });
          })
          .catch(function (err) {
            that.loading = false;
            console.log(error);
            that.$message.error({
              showClose: true,
              message: "请求出现异常",
              duration: 2000
            });
          });
        console.log(data)
      },
      deleted:function (data) {
        let that = this;
        that.dialogVisible = true;
        this.$confirm("确认删除该记录吗?", "提示", {type: "warning"})
          .then(()=>{
            that.loading = true;
            let params = {id:data.id};
            console.log(params)
            API_ESCORT_ENTERPRISE.deleteEnterprise(params)
              .then(
                function (result) {
                  that.loading = false;
                  if (result && parseInt(result.code) === 0) {
                    that.$message.success({
                      showClose: true,
                      message: "删除成功",
                      duration: 1500
                    });
                    that.search();
                  }
                },
                function (err) {
                  console.log(err)
                  that.loading = false;
                  that.$message.error({
                    showClose: true,
                    message: err,
                    duration: 2000
                  });
                }
              )
              .catch(function (error) {
                that.loading = false;
                console.log(error);
                that.$message.error({
                showClose: true,
                message: "请求出现异常",
                duration: 2000
              });
            })
          })
      },
      detail:function(data){
        console.log("detail");
        let that = this;
        that.loading = true;
        let param = {
          id : data
        }
        API_ESCORT_ENTERPRISE.getById(param).then(function (result) {
          that.loading = false;
          console.log(result);
          if (0 == result.code){
            that.detailForm = result.info;
            that.detailVisible = true;
          } else{
            that.$message.error({
              showClose: true,
              message: "请求数据失败",
              duration: 2000
            });
          }
        }).catch(function (err) {
          that.loading = false;
          console.log(error);
          that.$message.error({
            showClose: true,
            message: "请求出现异常",
            duration: 2000
          });
        });
      }
    },
    mounted() {
      this.handleSearch();
    }
  };
</script>

<style scoped>

</style>
